﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public enum TowerType
{
    None,
    Archer,
    Mage,
    War,
    Turret
}
public class Tower:MonoBehaviour
{
    public int attack  = 10;
    public float attackSpeed  = 1;
    public int price  = 5;
    public int level  = 1;
    public float attackRange  = 15;
    public int upPrice = 10;
    public TowerType towerType  = TowerType.None;
    
    public Action<int> onLevelChange;
    protected GameObject target;

    public virtual void Awake() {
        
    }
    /// <summary>
    /// 升级
    /// </summary>
    public virtual  GameObject Up()
    {
        if (level>=3)
        {
            return null;
        }
        level++;
        if (onLevelChange != null)
        {
            onLevelChange(level);
        }
        CaculateValue();
        return gameObject;
    }
    /// <summary>
    /// 出售
    /// </summary>
    public virtual void Sell()
    {
        GameManager.Instance.RemoveTower(this.gameObject);
        Destroy(this.gameObject);
        GameManager.Instance.AddCoin(Mathf.RoundToInt(price*0.8f));
    }

    /// <summary>
    /// 计算升级数值
    /// </summary>
    protected virtual void CaculateValue()
    {
        attack = level * 10;
        attackSpeed = level * 1.2f;
        price = level * 5;
    }
    



    protected virtual void FindNearestTargetEnemy(){
        List<GameObject> objs= GameManager.Instance.allEnemys;
        float distance=99999;
        for(int i=0,count=objs.Count;i<count;i++){
            float currDis=(objs[i].transform.position-transform.position).magnitude;
            if(distance<currDis){
                distance=currDis;
                target=objs[i];
            }
        }
        Debug.Log("FindTargetEnemy");
    }
    protected virtual void FindTargetEnemy(){
         List<GameObject> objs= GameManager.Instance.allEnemys;
        for(int i=0,count=objs.Count;i<count;i++){
            float currDis=(objs[i].transform.position-transform.position).magnitude;
            if(attackRange>currDis){
                target=objs[i];
                break;
            }
        }
    }

    float t=0;
    protected virtual void Attack(){

        Debug.Log("Attack");
    }
    protected virtual void Update(){
        t-=Time.deltaTime;
        if(target!=null){
            float dist=(target.transform.position-transform.position).magnitude;
            if(dist<attackRange){
                Vector3 lookPoint= new Vector3(target.transform.position.x-transform.position.x,0,target.transform.position.z-transform.position.z);
                Quaternion look=Quaternion.LookRotation(lookPoint);
                transform.rotation=Quaternion.Lerp(transform.rotation,look,Time.deltaTime*30);
                if(t<0){
                    Attack();
                    t=attackSpeed;
                }
                
            }
            else{
                FindTargetEnemy();
            }

        }
        else{
            FindTargetEnemy();
        }
    }
}

